package com.example.weather;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.weather.db.DBManager;
import com.example.weather.db.DateBaseBean;

import java.util.ArrayList;
import java.util.List;

public class CityManagerActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener{
    private static final String TAG = "CityManagerActivity";
    ImageView addIv;
    ListView cityLv;
    List<DateBaseBean> mDatas;//从数据库显示列表数据源
    List<String> mDatasCity;
    private CityManagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);
        addIv = findViewById(R.id.city_iv_add);
        cityLv = findViewById(R.id.city_lv);
        mDatas = new ArrayList<>();
        mDatasCity=new ArrayList<>();
        cityLv.setOnItemLongClickListener(this);
        //设置适配器
        adapter=new CityManagerAdapter(this,mDatas);
        cityLv.setAdapter(adapter);
    }
    //适配器更新
    @Override
    protected void onResume() {
        super.onResume();
        List<DateBaseBean> list = DBManager.queryAllInfo();
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
        Log.i(TAG, "onResume: 更新后的list"+list);

    }
    public void addcity(View v){
        int cityCount = DBManager.getCityCount();
        if (cityCount<10) {
            Intent intent = new Intent(this, SearchCityActivity.class);
            startActivity(intent);
        }else {
            Toast.makeText(this,"存储城市数量已达上限，请删除后再增加",Toast.LENGTH_SHORT).show();
        }
    }
//长按删除功能
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: 长按事件 position="+position);
        DateBaseBean item=(DateBaseBean) cityLv.getItemAtPosition(position);
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: 对话框事件处理");
                        //删除数据项
                        adapter.remove(position);
                        List<String> cityList=DBManager.queryAllCityName();
                        Log.i(TAG, "delete: 删除之前的城市列表: "+cityList);
                        mDatasCity.addAll(cityList);
                        String city=cityList.get(position);
                        DBManager.deleteInfoByCity(city);
                        adapter.notifyDataSetChanged();
                        Log.i(TAG, "delete: 删除选中后的城市列表"+cityList);
                    }
                }).setNegativeButton("取消",null);
        builder.create().show();

         return true;
    }
}